<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Making a custom type_index</title>
<link rel="stylesheet" href="../../../doc/src/boostbook.css" type="text/css">
<meta name="generator" content="DocBook XSL Stylesheets V1.79.1">
<link rel="home" href="../index.html" title="The Boost C++ Libraries BoostBook Documentation Subset">
<link rel="up" href="../boost_typeindex.html" title="Chapter 42. Boost.TypeIndex 4.1">
<link rel="prev" href="../boost/typeindex/hash_value.html" title="Function template hash_value">
<link rel="next" href="space_and_performance.html" title="Space and Performance">
</head>
<body bgcolor="white" text="black" link="#0000FF" vlink="#840084" alink="#0000FF">
<table cellpadding="2" width="100%"><tr>
<td valign="top"><img alt="Boost C++ Libraries" width="277" height="86" src="../../../boost.png"></td>
<td align="center"><a href="../../../index.html">Home</a></td>
<td align="center"><a href="../../../libs/libraries.htm">Libraries</a></td>
<td align="center"><a href="http://www.boost.org/users/people.html">People</a></td>
<td align="center"><a href="http://www.boost.org/users/faq.html">FAQ</a></td>
<td align="center"><a href="../../../more/index.htm">More</a></td>
</tr></table>
<hr>
<div class="spirit-nav">
<a accesskey="p" href="../boost/typeindex/hash_value.html"><img src="../../../doc/src/images/prev.png" alt="Prev"></a><a accesskey="u" href="../boost_typeindex.html"><img src="../../../doc/src/images/up.png" alt="Up"></a><a accesskey="h" href="../index.html"><img src="../../../doc/src/images/home.png" alt="Home"></a><a accesskey="n" href="space_and_performance.html"><img src="../../../doc/src/images/next.png" alt="Next"></a>
</div>
<div class="section">
<div class="titlepage"><div><div><h2 class="title" style="clear: both">
<a name="boost_typeindex.making_a_custom_type_index"></a><a class="link" href="making_a_custom_type_index.html" title="Making a custom type_index">Making a custom
    type_index</a>
</h2></div></div></div>
<div class="toc"><dl class="toc">
<dt><span class="section"><a href="making_a_custom_type_index.html#boost_typeindex.making_a_custom_type_index.basics">Basics</a></span></dt>
<dt><span class="section"><a href="making_a_custom_type_index.html#boost_typeindex.making_a_custom_type_index.getting_type_infos_at_runtime">Getting
      type infos at runtime</a></span></dt>
<dt><span class="section"><a href="making_a_custom_type_index.html#boost_typeindex.making_a_custom_type_index.using_new_type_infos_all_around_">Using
      new type infos all around the code</a></span></dt>
</dl></div>
<p>
      Sometimes there may be a need to create your own type info system. This may
      be useful if you wish to store some more info about types (PODness, size of
      a type, pointers to common functions...) or if you have an idea of a more compact
      types representations.
    </p>
<div class="section">
<div class="titlepage"><div><div><h3 class="title">
<a name="boost_typeindex.making_a_custom_type_index.basics"></a><a class="link" href="making_a_custom_type_index.html#boost_typeindex.making_a_custom_type_index.basics" title="Basics">Basics</a>
</h3></div></div></div>
<p>
        The following example shows how a user defined type_info can be created and
        used. Example works with and without RTTI.
      </p>
<p>
        Consider situation when user uses only those types in <code class="computeroutput"><span class="keyword">typeid</span><span class="special">()</span></code>:
      </p>
<pre class="programlisting"><span class="preprocessor">#include</span> <span class="special">&lt;</span><span class="identifier">vector</span><span class="special">&gt;</span>
<span class="preprocessor">#include</span> <span class="special">&lt;</span><span class="identifier">string</span><span class="special">&gt;</span>

<span class="keyword">namespace</span> <span class="identifier">my_namespace</span> <span class="special">{</span>

<span class="keyword">class</span> <span class="identifier">my_class</span><span class="special">;</span>
<span class="keyword">struct</span> <span class="identifier">my_struct</span><span class="special">;</span>

<span class="keyword">typedef</span> <span class="identifier">std</span><span class="special">::</span><span class="identifier">vector</span><span class="special">&lt;</span><span class="identifier">my_class</span><span class="special">&gt;</span> <span class="identifier">my_classes</span><span class="special">;</span>
<span class="keyword">typedef</span> <span class="identifier">std</span><span class="special">::</span><span class="identifier">string</span> <span class="identifier">my_string</span><span class="special">;</span>

<span class="special">}</span> <span class="comment">// namespace my_namespace</span>
</pre>
<p>
        In that case user may wish to save space in binary and create it's own type
        system. For that case <code class="computeroutput"><span class="identifier">detail</span><span class="special">::</span><span class="identifier">typenum</span><span class="special">&lt;&gt;</span></code> meta function is added. Depending
        on the input type T this function will return different numeric values.
      </p>
<pre class="programlisting"><span class="preprocessor">#include</span> <span class="special">&lt;</span><span class="identifier">boost</span><span class="special">/</span><span class="identifier">type_index</span><span class="special">/</span><span class="identifier">type_index_facade</span><span class="special">.</span><span class="identifier">hpp</span><span class="special">&gt;</span>

<span class="keyword">namespace</span> <span class="identifier">my_namespace</span> <span class="special">{</span> <span class="keyword">namespace</span> <span class="identifier">detail</span> <span class="special">{</span>
    <span class="keyword">template</span> <span class="special">&lt;</span><span class="keyword">class</span> <span class="identifier">T</span><span class="special">&gt;</span> <span class="keyword">struct</span> <span class="identifier">typenum</span><span class="special">;</span>
    <span class="keyword">template</span> <span class="special">&lt;&gt;</span> <span class="keyword">struct</span> <span class="identifier">typenum</span><span class="special">&lt;</span><span class="keyword">void</span><span class="special">&gt;{</span>       <span class="keyword">enum</span> <span class="special">{</span><span class="identifier">value</span> <span class="special">=</span> <span class="number">0</span><span class="special">};</span> <span class="special">};</span>
    <span class="keyword">template</span> <span class="special">&lt;&gt;</span> <span class="keyword">struct</span> <span class="identifier">typenum</span><span class="special">&lt;</span><span class="identifier">my_class</span><span class="special">&gt;{</span>   <span class="keyword">enum</span> <span class="special">{</span><span class="identifier">value</span> <span class="special">=</span> <span class="number">1</span><span class="special">};</span> <span class="special">};</span>
    <span class="keyword">template</span> <span class="special">&lt;&gt;</span> <span class="keyword">struct</span> <span class="identifier">typenum</span><span class="special">&lt;</span><span class="identifier">my_struct</span><span class="special">&gt;{</span>  <span class="keyword">enum</span> <span class="special">{</span><span class="identifier">value</span> <span class="special">=</span> <span class="number">2</span><span class="special">};</span> <span class="special">};</span>
    <span class="keyword">template</span> <span class="special">&lt;&gt;</span> <span class="keyword">struct</span> <span class="identifier">typenum</span><span class="special">&lt;</span><span class="identifier">my_classes</span><span class="special">&gt;{</span> <span class="keyword">enum</span> <span class="special">{</span><span class="identifier">value</span> <span class="special">=</span> <span class="number">3</span><span class="special">};</span> <span class="special">};</span>
    <span class="keyword">template</span> <span class="special">&lt;&gt;</span> <span class="keyword">struct</span> <span class="identifier">typenum</span><span class="special">&lt;</span><span class="identifier">my_string</span><span class="special">&gt;{</span>  <span class="keyword">enum</span> <span class="special">{</span><span class="identifier">value</span> <span class="special">=</span> <span class="number">4</span><span class="special">};</span> <span class="special">};</span>

<span class="preprocessor">#ifdef</span> <span class="identifier">BOOST_MSVC</span>
<span class="preprocessor">#pragma</span> <span class="identifier">warning</span><span class="special">(</span><span class="identifier">push</span><span class="special">)</span>
<span class="preprocessor">#pragma</span> <span class="identifier">warning</span><span class="special">(</span><span class="identifier">disable</span><span class="special">:</span> <span class="number">4510</span> <span class="number">4512</span> <span class="number">4610</span><span class="special">)</span> <span class="comment">// non-copyable non-constructable type</span>
<span class="preprocessor">#endif</span>

    <span class="comment">// my_typeinfo structure is used to save type number</span>
    <span class="keyword">struct</span> <span class="identifier">my_typeinfo</span> <span class="special">{</span>
        <span class="keyword">const</span> <span class="keyword">char</span><span class="special">*</span> <span class="keyword">const</span> <span class="identifier">type_</span><span class="special">;</span>
    <span class="special">};</span>

<span class="preprocessor">#ifdef</span> <span class="identifier">BOOST_MSVC</span>
<span class="preprocessor">#pragma</span> <span class="identifier">warning</span><span class="special">(</span><span class="identifier">pop</span><span class="special">)</span>
<span class="preprocessor">#endif</span>

    <span class="keyword">const</span> <span class="identifier">my_typeinfo</span> <span class="identifier">infos</span><span class="special">[</span><span class="number">5</span><span class="special">]</span> <span class="special">=</span> <span class="special">{</span>
        <span class="special">{</span><span class="string">"void"</span><span class="special">},</span> <span class="special">{</span><span class="string">"my_class"</span><span class="special">},</span> <span class="special">{</span><span class="string">"my_struct"</span><span class="special">},</span> <span class="special">{</span><span class="string">"my_classes"</span><span class="special">},</span> <span class="special">{</span><span class="string">"my_string"</span><span class="special">}</span>
    <span class="special">};</span>

    <span class="keyword">template</span> <span class="special">&lt;</span><span class="keyword">class</span> <span class="identifier">T</span><span class="special">&gt;</span>
    <span class="keyword">inline</span> <span class="keyword">const</span> <span class="identifier">my_typeinfo</span><span class="special">&amp;</span> <span class="identifier">my_typeinfo_construct</span><span class="special">()</span> <span class="special">{</span>
        <span class="keyword">return</span> <span class="identifier">infos</span><span class="special">[</span><span class="identifier">typenum</span><span class="special">&lt;</span><span class="identifier">T</span><span class="special">&gt;::</span><span class="identifier">value</span><span class="special">];</span>
    <span class="special">}</span>
<span class="special">}}</span> <span class="comment">// my_namespace::detail</span>
</pre>
<p>
        <code class="computeroutput"><span class="identifier">my_type_index</span></code> is a user created
        type_index class. If in doubt during this phase, you can always take a look
        at the <code class="computeroutput"><span class="special">&lt;</span><span class="identifier">boost</span><span class="special">/</span><span class="identifier">type_index</span><span class="special">/</span><span class="identifier">ctti_type_index</span><span class="special">.</span><span class="identifier">hpp</span><span class="special">&gt;</span></code>
        or <code class="computeroutput"><span class="special">&lt;</span><span class="identifier">boost</span><span class="special">/</span><span class="identifier">type_index</span><span class="special">/</span><span class="identifier">stl_type_index</span><span class="special">.</span><span class="identifier">hpp</span><span class="special">&gt;</span></code>
        files. Documentation for <code class="computeroutput"><span class="identifier">type_index_facade</span></code>
        could be also useful.
      </p>
<p>
        Since we are not going to override <code class="computeroutput"><span class="identifier">type_index_facade</span><span class="special">::</span><span class="identifier">hash_code</span><span class="special">()</span></code> we must additionally include <code class="computeroutput"><span class="special">&lt;</span><span class="identifier">boost</span><span class="special">/</span><span class="identifier">container_hash</span><span class="special">/</span><span class="identifier">hash</span><span class="special">.</span><span class="identifier">hpp</span><span class="special">&gt;</span></code>.
      </p>
<pre class="programlisting"><span class="preprocessor">#include</span> <span class="special">&lt;</span><span class="identifier">boost</span><span class="special">/</span><span class="identifier">container_hash</span><span class="special">/</span><span class="identifier">hash</span><span class="special">.</span><span class="identifier">hpp</span><span class="special">&gt;</span>
</pre>
<p>
        See implementation of <code class="computeroutput"><span class="identifier">my_type_index</span></code>:
      </p>
<pre class="programlisting"><span class="keyword">namespace</span> <span class="identifier">my_namespace</span> <span class="special">{</span>

<span class="keyword">class</span> <span class="identifier">my_type_index</span><span class="special">:</span> <span class="keyword">public</span> <span class="identifier">boost</span><span class="special">::</span><span class="identifier">typeindex</span><span class="special">::</span><span class="identifier">type_index_facade</span><span class="special">&lt;</span><span class="identifier">my_type_index</span><span class="special">,</span> <span class="identifier">detail</span><span class="special">::</span><span class="identifier">my_typeinfo</span><span class="special">&gt;</span> <span class="special">{</span>
    <span class="keyword">const</span> <span class="identifier">detail</span><span class="special">::</span><span class="identifier">my_typeinfo</span><span class="special">*</span> <span class="identifier">data_</span><span class="special">;</span>

<span class="keyword">public</span><span class="special">:</span>
    <span class="keyword">typedef</span> <span class="identifier">detail</span><span class="special">::</span><span class="identifier">my_typeinfo</span> <span class="identifier">type_info_t</span><span class="special">;</span>

    <span class="keyword">inline</span> <span class="identifier">my_type_index</span><span class="special">()</span> <span class="identifier">BOOST_NOEXCEPT</span>
        <span class="special">:</span> <span class="identifier">data_</span><span class="special">(&amp;</span><span class="identifier">detail</span><span class="special">::</span><span class="identifier">my_typeinfo_construct</span><span class="special">&lt;</span><span class="keyword">void</span><span class="special">&gt;())</span>
    <span class="special">{}</span>

    <span class="keyword">inline</span> <span class="identifier">my_type_index</span><span class="special">(</span><span class="keyword">const</span> <span class="identifier">type_info_t</span><span class="special">&amp;</span> <span class="identifier">data</span><span class="special">)</span> <span class="identifier">BOOST_NOEXCEPT</span>
        <span class="special">:</span> <span class="identifier">data_</span><span class="special">(&amp;</span><span class="identifier">data</span><span class="special">)</span>
    <span class="special">{}</span>

    <span class="keyword">inline</span> <span class="keyword">const</span> <span class="identifier">type_info_t</span><span class="special">&amp;</span>  <span class="identifier">type_info</span><span class="special">()</span> <span class="keyword">const</span> <span class="identifier">BOOST_NOEXCEPT</span> <span class="special">{</span>
        <span class="keyword">return</span> <span class="special">*</span><span class="identifier">data_</span><span class="special">;</span>
    <span class="special">}</span>

    <span class="keyword">inline</span> <span class="keyword">const</span> <span class="keyword">char</span><span class="special">*</span>  <span class="identifier">raw_name</span><span class="special">()</span> <span class="keyword">const</span> <span class="identifier">BOOST_NOEXCEPT</span> <span class="special">{</span>
        <span class="keyword">return</span> <span class="identifier">data_</span><span class="special">-&gt;</span><span class="identifier">type_</span><span class="special">;</span>
    <span class="special">}</span>

    <span class="keyword">inline</span> <span class="identifier">std</span><span class="special">::</span><span class="identifier">string</span>  <span class="identifier">pretty_name</span><span class="special">()</span> <span class="keyword">const</span> <span class="special">{</span>
        <span class="keyword">return</span> <span class="identifier">data_</span><span class="special">-&gt;</span><span class="identifier">type_</span><span class="special">;</span>
    <span class="special">}</span>

    <span class="keyword">template</span> <span class="special">&lt;</span><span class="keyword">class</span> <span class="identifier">T</span><span class="special">&gt;</span>
    <span class="keyword">inline</span> <span class="keyword">static</span> <span class="identifier">my_type_index</span> <span class="identifier">type_id</span><span class="special">()</span> <span class="identifier">BOOST_NOEXCEPT</span> <span class="special">{</span>
        <span class="keyword">return</span> <span class="identifier">detail</span><span class="special">::</span><span class="identifier">my_typeinfo_construct</span><span class="special">&lt;</span><span class="identifier">T</span><span class="special">&gt;();</span>
    <span class="special">}</span>

    <span class="keyword">template</span> <span class="special">&lt;</span><span class="keyword">class</span> <span class="identifier">T</span><span class="special">&gt;</span>
    <span class="keyword">inline</span> <span class="keyword">static</span> <span class="identifier">my_type_index</span> <span class="identifier">type_id_with_cvr</span><span class="special">()</span> <span class="identifier">BOOST_NOEXCEPT</span> <span class="special">{</span>
        <span class="keyword">return</span> <span class="identifier">detail</span><span class="special">::</span><span class="identifier">my_typeinfo_construct</span><span class="special">&lt;</span><span class="identifier">T</span><span class="special">&gt;();</span>
    <span class="special">}</span>

    <span class="keyword">template</span> <span class="special">&lt;</span><span class="keyword">class</span> <span class="identifier">T</span><span class="special">&gt;</span>
    <span class="keyword">inline</span> <span class="keyword">static</span> <span class="identifier">my_type_index</span> <span class="identifier">type_id_runtime</span><span class="special">(</span><span class="keyword">const</span> <span class="identifier">T</span><span class="special">&amp;</span> <span class="identifier">variable</span><span class="special">)</span> <span class="identifier">BOOST_NOEXCEPT</span><span class="special">;</span>
<span class="special">};</span>

<span class="special">}</span> <span class="comment">// namespace my_namespace</span>
</pre>
<p>
        Note that we have used the boost::typeindex::type_index_facade class as base.
        That class took care about all the helper function and operators (comparison,
        hashing, ostreaming and others).
      </p>
<p>
        Finally we can use the my_type_index class for getting type indexes:
      </p>
<pre class="programlisting"><span class="identifier">my_type_index</span>
    <span class="identifier">cl1</span> <span class="special">=</span> <span class="identifier">my_type_index</span><span class="special">::</span><span class="identifier">type_id</span><span class="special">&lt;</span><span class="identifier">my_class</span><span class="special">&gt;(),</span>
    <span class="identifier">st1</span> <span class="special">=</span> <span class="identifier">my_type_index</span><span class="special">::</span><span class="identifier">type_id</span><span class="special">&lt;</span><span class="identifier">my_struct</span><span class="special">&gt;(),</span>
    <span class="identifier">st2</span> <span class="special">=</span> <span class="identifier">my_type_index</span><span class="special">::</span><span class="identifier">type_id</span><span class="special">&lt;</span><span class="identifier">my_struct</span><span class="special">&gt;(),</span>
    <span class="identifier">vec</span> <span class="special">=</span> <span class="identifier">my_type_index</span><span class="special">::</span><span class="identifier">type_id</span><span class="special">&lt;</span><span class="identifier">my_classes</span><span class="special">&gt;()</span>
<span class="special">;</span>

<span class="identifier">assert</span><span class="special">(</span><span class="identifier">cl1</span> <span class="special">!=</span> <span class="identifier">st1</span><span class="special">);</span>
<span class="identifier">assert</span><span class="special">(</span><span class="identifier">st2</span> <span class="special">==</span> <span class="identifier">st1</span><span class="special">);</span>
<span class="identifier">assert</span><span class="special">(</span><span class="identifier">vec</span><span class="special">.</span><span class="identifier">pretty_name</span><span class="special">()</span> <span class="special">==</span> <span class="string">"my_classes"</span><span class="special">);</span>
<span class="identifier">assert</span><span class="special">(</span><span class="identifier">cl1</span><span class="special">.</span><span class="identifier">pretty_name</span><span class="special">()</span> <span class="special">==</span> <span class="string">"my_class"</span><span class="special">);</span>
</pre>
</div>
<div class="section">
<div class="titlepage"><div><div><h3 class="title">
<a name="boost_typeindex.making_a_custom_type_index.getting_type_infos_at_runtime"></a><a class="link" href="making_a_custom_type_index.html#boost_typeindex.making_a_custom_type_index.getting_type_infos_at_runtime" title="Getting type infos at runtime">Getting
      type infos at runtime</a>
</h3></div></div></div>
<p>
        Usually to allow runtime type info we need to register class with some macro.
        Let's see how a <code class="computeroutput"><span class="identifier">MY_TYPEINDEX_REGISTER_CLASS</span></code>
        macro could be implemented for our <code class="computeroutput"><span class="identifier">my_type_index</span></code>
        class:
      </p>
<pre class="programlisting"><span class="keyword">namespace</span> <span class="identifier">my_namespace</span> <span class="special">{</span> <span class="keyword">namespace</span> <span class="identifier">detail</span> <span class="special">{</span>

    <span class="keyword">template</span> <span class="special">&lt;</span><span class="keyword">class</span> <span class="identifier">T</span><span class="special">&gt;</span>
    <span class="keyword">inline</span> <span class="keyword">const</span> <span class="identifier">my_typeinfo</span><span class="special">&amp;</span> <span class="identifier">my_typeinfo_construct_ref</span><span class="special">(</span><span class="keyword">const</span> <span class="identifier">T</span><span class="special">*)</span> <span class="special">{</span>
        <span class="keyword">return</span> <span class="identifier">my_typeinfo_construct</span><span class="special">&lt;</span><span class="identifier">T</span><span class="special">&gt;();</span>
    <span class="special">}</span>

<span class="preprocessor">#define</span> <span class="identifier">MY_TYPEINDEX_REGISTER_CLASS</span>                                             <span class="special">\</span>
    <span class="keyword">virtual</span> <span class="keyword">const</span> <span class="identifier">my_namespace</span><span class="special">::</span><span class="identifier">detail</span><span class="special">::</span><span class="identifier">my_typeinfo</span><span class="special">&amp;</span> <span class="identifier">type_id_runtime</span><span class="special">()</span> <span class="keyword">const</span> <span class="special">{</span>  <span class="special">\</span>
        <span class="keyword">return</span> <span class="identifier">my_namespace</span><span class="special">::</span><span class="identifier">detail</span><span class="special">::</span><span class="identifier">my_typeinfo_construct_ref</span><span class="special">(</span><span class="keyword">this</span><span class="special">);</span>           <span class="special">\</span>
    <span class="special">}</span>

<span class="special">}}</span> <span class="comment">// namespace my_namespace::detail</span>
</pre>
<p>
        Now when we have a MY_TYPEINDEX_REGISTER_CLASS, let's implement a <code class="computeroutput"><span class="identifier">my_type_index</span><span class="special">::</span><span class="identifier">type_id_runtime</span></code> method:
      </p>
<pre class="programlisting"><span class="keyword">namespace</span> <span class="identifier">my_namespace</span> <span class="special">{</span>
    <span class="keyword">template</span> <span class="special">&lt;</span><span class="keyword">class</span> <span class="identifier">T</span><span class="special">&gt;</span>
    <span class="identifier">my_type_index</span> <span class="identifier">my_type_index</span><span class="special">::</span><span class="identifier">type_id_runtime</span><span class="special">(</span><span class="keyword">const</span> <span class="identifier">T</span><span class="special">&amp;</span> <span class="identifier">variable</span><span class="special">)</span> <span class="identifier">BOOST_NOEXCEPT</span> <span class="special">{</span>
        <span class="comment">// Classes that were marked with `MY_TYPEINDEX_REGISTER_CLASS` will have a</span>
        <span class="comment">// `type_id_runtime()` method.</span>
        <span class="keyword">return</span> <span class="identifier">variable</span><span class="special">.</span><span class="identifier">type_id_runtime</span><span class="special">();</span>
    <span class="special">}</span>
<span class="special">}</span>
</pre>
<p>
        Consider the situation, when <code class="computeroutput"><span class="identifier">my_class</span></code>
        and <code class="computeroutput"><span class="identifier">my_struct</span></code> are polymorphic
        classes:
      </p>
<pre class="programlisting"><span class="keyword">namespace</span> <span class="identifier">my_namespace</span> <span class="special">{</span>

<span class="keyword">class</span> <span class="identifier">my_class</span> <span class="special">{</span>
<span class="keyword">public</span><span class="special">:</span>
    <span class="identifier">MY_TYPEINDEX_REGISTER_CLASS</span>
    <span class="keyword">virtual</span> <span class="special">~</span><span class="identifier">my_class</span><span class="special">()</span> <span class="special">{}</span>
<span class="special">};</span>

<span class="keyword">struct</span> <span class="identifier">my_struct</span><span class="special">:</span> <span class="keyword">public</span> <span class="identifier">my_class</span> <span class="special">{</span>
    <span class="identifier">MY_TYPEINDEX_REGISTER_CLASS</span>
<span class="special">};</span>

<span class="special">}</span> <span class="comment">// namespace my_namespace</span>
</pre>
<p>
        Now the following example will compile and work.
      </p>
<pre class="programlisting"><span class="identifier">my_struct</span> <span class="identifier">str</span><span class="special">;</span>
<span class="identifier">my_class</span><span class="special">&amp;</span> <span class="identifier">reference</span> <span class="special">=</span> <span class="identifier">str</span><span class="special">;</span>
<span class="identifier">assert</span><span class="special">(</span><span class="identifier">my_type_index</span><span class="special">::</span><span class="identifier">type_id</span><span class="special">&lt;</span><span class="identifier">my_struct</span><span class="special">&gt;()</span> <span class="special">==</span> <span class="identifier">my_type_index</span><span class="special">::</span><span class="identifier">type_id_runtime</span><span class="special">(</span><span class="identifier">reference</span><span class="special">));</span>
</pre>
</div>
<div class="section">
<div class="titlepage"><div><div><h3 class="title">
<a name="boost_typeindex.making_a_custom_type_index.using_new_type_infos_all_around_"></a><a class="link" href="making_a_custom_type_index.html#boost_typeindex.making_a_custom_type_index.using_new_type_infos_all_around_" title="Using new type infos all around the code">Using
      new type infos all around the code</a>
</h3></div></div></div>
<p>
        There is an easy way to force <code class="computeroutput"><span class="identifier">boost</span><span class="special">::</span><span class="identifier">typeindex</span><span class="special">::</span><span class="identifier">type_id</span></code>
        to use your own type_index class.
      </p>
<p>
        All we need to do is just define <code class="computeroutput"><span class="identifier">BOOST_TYPE_INDEX_USER_TYPEINDEX</span></code>
        to the full path to header file of your type index class:
      </p>
<pre class="programlisting"><span class="comment">// BOOST_TYPE_INDEX_USER_TYPEINDEX must be defined *BEFORE* first inclusion of &lt;boost/type_index.hpp&gt;</span>
<span class="preprocessor">#define</span> <span class="identifier">BOOST_TYPE_INDEX_USER_TYPEINDEX</span> <span class="special">&lt;</span><span class="identifier">boost</span><span class="special">/../</span><span class="identifier">libs</span><span class="special">/</span><span class="identifier">type_index</span><span class="special">/</span><span class="identifier">examples</span><span class="special">/</span><span class="identifier">user_defined_typeinfo</span><span class="special">.</span><span class="identifier">hpp</span><span class="special">&gt;</span>
<span class="preprocessor">#include</span> <span class="special">&lt;</span><span class="identifier">boost</span><span class="special">/</span><span class="identifier">type_index</span><span class="special">.</span><span class="identifier">hpp</span><span class="special">&gt;</span>
</pre>
<p>
        You'll also need to add some typedefs and macro to your "user_defined_typeinfo.hpp"
        header file:
      </p>
<pre class="programlisting"><span class="preprocessor">#define</span> <span class="identifier">BOOST_TYPE_INDEX_REGISTER_CLASS</span> <span class="identifier">MY_TYPEINDEX_REGISTER_CLASS</span>
<span class="keyword">namespace</span> <span class="identifier">boost</span> <span class="special">{</span> <span class="keyword">namespace</span> <span class="identifier">typeindex</span> <span class="special">{</span>
    <span class="keyword">typedef</span> <span class="identifier">my_namespace</span><span class="special">::</span><span class="identifier">my_type_index</span> <span class="identifier">type_index</span><span class="special">;</span>
<span class="special">}}</span>
</pre>
<p>
        That's it! Now all TypeIndex global methods and typedefs will be using your
        class:
      </p>
<pre class="programlisting"><span class="identifier">boost</span><span class="special">::</span><span class="identifier">typeindex</span><span class="special">::</span><span class="identifier">type_index</span> <span class="identifier">worldwide</span> <span class="special">=</span> <span class="identifier">boost</span><span class="special">::</span><span class="identifier">typeindex</span><span class="special">::</span><span class="identifier">type_id</span><span class="special">&lt;</span><span class="identifier">my_classes</span><span class="special">&gt;();</span>
<span class="identifier">assert</span><span class="special">(</span><span class="identifier">worldwide</span><span class="special">.</span><span class="identifier">pretty_name</span><span class="special">()</span> <span class="special">==</span> <span class="string">"my_classes"</span><span class="special">);</span>
<span class="identifier">assert</span><span class="special">(</span><span class="identifier">worldwide</span> <span class="special">==</span> <span class="identifier">my_type_index</span><span class="special">::</span><span class="identifier">type_id</span><span class="special">&lt;</span><span class="identifier">my_classes</span><span class="special">&gt;());</span>
</pre>
</div>
</div>
<table xmlns:rev="http://www.cs.rpi.edu/~gregod/boost/tools/doc/revision" width="100%"><tr>
<td align="left"></td>
<td align="right"><div class="copyright-footer">Copyright © 2012-2021 Antony Polukhin<p>
        Distributed under the Boost Software License, Version 1.0. (See accompanying
        file LICENSE_1_0.txt or copy at <a href="http://www.boost.org/LICENSE_1_0.txt" target="_top">http://www.boost.org/LICENSE_1_0.txt</a>)
      </p>
</div></td>
</tr></table>
<hr>
<div class="spirit-nav">
<a accesskey="p" href="../boost/typeindex/hash_value.html"><img src="../../../doc/src/images/prev.png" alt="Prev"></a><a accesskey="u" href="../boost_typeindex.html"><img src="../../../doc/src/images/up.png" alt="Up"></a><a accesskey="h" href="../index.html"><img src="../../../doc/src/images/home.png" alt="Home"></a><a accesskey="n" href="space_and_performance.html"><img src="../../../doc/src/images/next.png" alt="Next"></a>
</div>
</body>
</html>
